package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.Orders;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into orders\n" +
            "        (number, status, user_id, address_book_id, order_time, checkout_time, pay_method, pay_status, amount, remark,\n" +
            "         phone, address, consignee, estimated_delivery_time, delivery_status, pack_amount, tableware_number,\n" +
            "         tableware_status)\n" +
            "        values (#{number}, #{status}, #{userId}, #{addressBookId}, #{orderTime}, #{checkoutTime}, #{payMethod},\n" +
            "                #{payStatus}, #{amount}, #{remark}, #{phone}, #{address}, #{consignee},\n" +
            "                #{estimatedDeliveryTime}, #{deliveryStatus}, #{packAmount}, #{tablewareNumber}, #{tablewareStatus})")
    void insert(Orders order);

    Page<Orders> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);

    @Select("select * from orders where id = #{id}")
    Orders getOrderById(Long id);

    @Update("update orders set status = #{status},delivery_time = #{deliveryTime} where id = #{id}")
    void updateStatus(Orders orders);

    Page<Orders> getOrder(OrdersPageQueryDTO ordersPageQueryDTO);

    Integer countByStatus(Integer status);

    @Update("update orders set rejection_reason = #{rejectionReason},status = 6 where id = #{id}")
    void updateById(OrdersRejectionDTO ordersRejectionDTO);

    @Update("update orders set status = #{status},cancel_time = #{cancelTime},cancel_reason = #{cancelReason} where id = #{id}")
    void updateById2(Orders orders);

    @Select("select * from orders where status = #{status} and order_time < #{time}")
    List<Orders> processTimeoutOrder(LocalDateTime time, Integer status);

    @Update("update orders set status = #{status},delivery_time = #{deliveryTime} where id = #{id}")
    void update(Orders order);

    @Update("update orders set status = #{status} where id = #{id}")
    void update2(Orders orders);

    List<Orders> ByStatus(LocalDateTime start, LocalDateTime end);

    int getCount(int i, LocalDateTime start, LocalDateTime end);

    int getCount2(LocalDateTime start, LocalDateTime end);

    Double getTurnover(Map map);

    Integer count(Map map);

    List<GoodsSalesDTO> top10();
}
